//
//  SmallDecisionsNswcSetViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/20.
//

#import "SmallDecisionsNswcSetViewController.h"
#import "SmallDecisionsNswcViewController.h"
#import "SmallDecisionsTextField.h"
#import "SmallDecisionsActionSheetViewController.h"

@interface SmallDecisionsNswcSetViewController ()
@property(nonatomic,strong) UIView *containerView;
@property(nonatomic,strong) SmallDecisionsTextField *typeTextField;
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) UILabel *contentLabel;
@property(nonatomic,strong) UIButton *sureBtn;
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, assign) NSUInteger selectIndex;

@end

@implementation SmallDecisionsNswcSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"你说我猜";
    [self containerView];
    [self typeTextField];
    [self titleLabel];
    [self contentLabel];
    [self slider];
    [self sureBtn];
    self.selectIndex = 0;
}

- (void)sliderValueChange:(UISlider *)slider {
    if(slider.value < 1) {
        self.contentLabel.text = @"30秒";
    } else {
        self.contentLabel.text = [NSString stringWithFormat:@"%.0f分钟",roundf(self.slider.value)];
    }
}

- (UIView *)containerView {
    if(!_containerView) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = SmallDecisionsPurpleColor;
        _containerView.layer.cornerRadius = 10;
        _containerView.layer.masksToBounds = YES;
        [self.view addSubview:_containerView];
        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.right.mas_equalTo(-30);
            make.center.mas_equalTo(0);
        }];
    }
    return _containerView;
}

- (UILabel *)titleLabel {
    if(!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = SmallDecisionsYellowColor;
        _titleLabel.text = @"设置";
        _titleLabel.font = SmallDecisionsSetBoldFontSize(22);
        _titleLabel.textAlignment = 1;
        [self.containerView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
    }
    return _titleLabel;
}


- (SmallDecisionsTextField *)typeTextField {
    if(!_typeTextField) {
        _typeTextField = [[SmallDecisionsTextField alloc] init];
        _typeTextField.leadingViewRect = CGRectMake(15, 18, 98, 20);
        _typeTextField.trailingViewRect = CGRectMake(0, 0, 20, 56);
        _typeTextField.leadingViewBtn.alignType = AlignType_TextRight;
        _typeTextField.leadingViewBtn.padding = 5;
        _typeTextField.leadingViewBtn.titleLabel.font = SmallDecisionsSetFontSize(16);
        _typeTextField.leadingViewBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _typeTextField.textAlignment = 2;
        [_typeTextField.leadingViewBtn setTitle:@"游戏类别" forState:UIControlStateNormal];
        [_typeTextField.leadingViewBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        _typeTextField.textColor = SmallDecisionsThemeFontColor;
        _typeTextField.font = SmallDecisionsSetFontSize(14);
        _typeTextField.backgroundColor = SmallDecisionsSecondBgColor;
        _typeTextField.layer.cornerRadius = 6;
        _typeTextField.layer.masksToBounds = YES;
        _typeTextField.enableInput = NO;
        [_typeTextField.trailingViewBtn setImage:[UIImage imageNamed:@"global_arrow_right"] forState:UIControlStateNormal];
        _typeTextField.text = @"随机";
        @weakify(self)
        _typeTextField.textFieldStartEditingAction = ^(SmallDecisionsTextField * _Nonnull textField) {
            @strongify(self)
            NSArray *list = @[@"随机", @"成语",@"动物名",@"食物名",@"明星名"];
            [SmallDecisionsActionSheetViewController showActionSheetWtihTitle:@"游戏类别" list:list cancelText:@"取消" selectedBlock:^(NSInteger index) {
                self.selectIndex = index;
                textField.text = list[index];
            } cancelBlock:nil objectToTextConverter:^NSString * _Nonnull(NSString *data) {
                return data;
            }];
        };
        
        [self.containerView addSubview:_typeTextField];
        [_typeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(30);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_equalTo(56);
        }];
    }
    return _typeTextField;
}

- (UILabel *)contentLabel {
    if(!_contentLabel) {
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.textColor = SmallDecisionsThemeFontColor;
        tipLabel.font = SmallDecisionsSetFontSize(14);
        tipLabel.text = @"时间设置";
        [self.containerView addSubview:tipLabel];
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.typeTextField.mas_bottom).mas_offset(20);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
        
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = SmallDecisionsThemeFontColor;
        _contentLabel.font = SmallDecisionsSetBoldFontSize(50);
        _contentLabel.numberOfLines = 0;
        _contentLabel.textAlignment = 1;
        _contentLabel.text = @"30秒";
        [self.containerView addSubview:_contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(tipLabel.mas_bottom).mas_offset(30);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
    }
    return _contentLabel;
}

- (UISlider *)slider {
    if(!_slider) {
        _slider = [[UISlider alloc] init];
        _slider.minimumTrackTintColor = SmallDecisionsThemeColor;
        [_slider setThumbImage:[UIImage imageNamed:@"slider_thumb_img"] forState:UIControlStateNormal];
        [_slider setThumbImage:[UIImage imageNamed:@"slider_thumb_img"] forState:UIControlStateFocused];
        [_slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
        _slider.continuous = YES;
        _slider.minimumValue = 0;
        _slider.maximumValue = 10;
        _slider.value = 0;
        [self.containerView addSubview:_slider];
        [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(30);
            make.left.mas_equalTo(20);
            make.height.mas_equalTo(50);
            make.right.mas_equalTo(-20);
        }];
    }

    return _slider;
}

- (UIButton *)sureBtn {
    if(!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _sureBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(18);
        _sureBtn.layer.cornerRadius = 25;
        _sureBtn.layer.masksToBounds = YES;
        _sureBtn.backgroundColor = SmallDecisionsThemeColor;
        [_sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:_sureBtn];
        [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.left.right.equalTo(self.slider);
            make.top.equalTo(self.slider.mas_bottom).offset(50);
            make.bottom.mas_offset(-30);
        }];
    }
    return _sureBtn;
}

- (void)sureBtnAction {
    if([SmallDecisionsSingletonHelper shareInstance].isVIP) {
        [self pushSzViewController];
    } else {
        NSUInteger number = [SmallDecisionsSingletonHelper getUserNumberWithKey:@"NSWCUSERNUMBER"];
        if(number > 3) {
            [SmallDecisionsSingletonHelper openVipViewController];
        } else {
            [SmallDecisionsSingletonHelper setUserNumberWithKey:@"NSWCUSERNUMBER" number:number + 1];
            [self pushSzViewController];
        }
    }
    
}

- (void)pushSzViewController {
    SmallDecisionsNswcViewController *nswcVC = [[SmallDecisionsNswcViewController alloc] init];
    nswcVC.time = self.slider.value == 0 ? 30: roundf(self.slider.value) * 60;
    nswcVC.selectIndex = self.selectIndex;
    [SmallDecisionsNavigatorManager pushViewController:nswcVC];
}


@end
